package com.mune.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mune.gulimall.product.dao.BrandDao;
import com.mune.gulimall.product.dao.CategoryDao;
import com.mune.gulimall.product.entity.BrandEntity;
import com.mune.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mune.common.utils.PageUtils;
import com.mune.common.utils.Query;

import com.mune.gulimall.product.dao.CategoryBrandRelationDao;
import com.mune.gulimall.product.entity.CategoryBrandRelationEntity;
import com.mune.gulimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
  @Resource
  private BrandDao brandDao;
  @Resource
  private CategoryDao categoryDao;
  @Resource
  CategoryBrandRelationDao relationDao;
  @Autowired
  BrandServiceImpl brandService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<CategoryBrandRelationEntity> page = this.page(
            new Query<CategoryBrandRelationEntity>().getPage(params),
            new QueryWrapper<CategoryBrandRelationEntity>()
    );

    return new PageUtils(page);
  }

  @Override
  public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
    Long brandId = categoryBrandRelation.getBrandId();
    Long catelogId = categoryBrandRelation.getCatelogId();

    BrandEntity brandEntity = brandDao.selectById(brandId);
    CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
    categoryBrandRelation.setBrandName(brandEntity.getName());
    categoryBrandRelation.setCatelogName(categoryEntity.getName());
    this.save(categoryBrandRelation);

  }

  // 更新冗余数据有多种方式,updateBrand是使用的逆向工程生成好的方法来进行更新
  // updateCategory是使用自己编写mapper的方式来进行更新
  @Override
  public void updateBrand(Long brandId, String name) {
    CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
    relationEntity.setBrandId(brandId);
    relationEntity.setBrandName(name);
    this.update(relationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
  }

  @Override
  public void updateCategory(Long catId, String name) {
    this.baseMapper.updateCategory(catId, name);
  }

  @Override
  public List<BrandEntity> getBrandsByCatId(Long catId) {

    List<CategoryBrandRelationEntity> catelogId = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
    List<BrandEntity> collect = catelogId.stream().map(item -> {
      Long brandId = item.getBrandId();
      return brandService.getById(brandId);
    }).collect(Collectors.toList());
    return collect;
  }
}